package com.heo.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.heo.pojo.CommandResult;
import com.heo.pojo.ControlInfoEntity;
import com.heo.pojo.DeviceInfoEntity;
import com.heo.pojo.DeviceLockInfoEntity;
import com.heo.pojo.OrgInfoEntity;
import com.heo.pojo.req.QueryDevicePara;
import com.heo.pojo.req.QueryDeviceStatusPara;
import com.heo.pub.DictEnum;
import com.heo.service.DeviceChannelService;
import com.heo.service.DeviceInfoService;
import com.heo.service.OrgInfoService;
import com.heo.util.RedisUtil;

/**
 * @author chichi
 */

@Controller
@Scope("singleton") // 1、为了性能。2、不需要多例。只要controller中不定义属性，那么单例完全是安全的。
@RequestMapping("/")
public class BaseController
{
  private static Logger log = Logger.getLogger(BaseController.class);

  public String[] statusConvertText(Byte status)
  {
    String[] result = new String[2];
    if (status == null)
    {
      result[0] = CommandResult.QueryDeviceResultCode.DEVICE_CHANNEL_INDEX_NOT_EXIST;
      result[1] = CommandResult.QueryDeviceResultMsg.DEVICE_CHANNEL_INDEX_NOT_EXIST;
      return result;
    }

    switch (status)
    {
      case DictEnum.DeviceChannelStatus.DEVICE_CHANNEL_ERROR:
        result[0] = CommandResult.QueryDeviceResultCode.DEVICE_CHANNEL_ERROR;
        result[1] = CommandResult.QueryDeviceResultMsg.DEVICE_CHANNEL_ERROR;
        return result;

      case DictEnum.DeviceChannelStatus.DEVICE_ERROR:
        result[0] = CommandResult.QueryDeviceResultCode.DEVICE_ERROR;
        result[1] = CommandResult.QueryDeviceResultMsg.DEVICE_ERROR;
        return result;

      case DictEnum.DeviceChannelStatus.FREE:
        result[0] = CommandResult.QueryDeviceResultCode.FREE;
        result[1] = CommandResult.QueryDeviceResultMsg.FREE;
        return result;

      case DictEnum.DeviceChannelStatus.START_SUCCESS:
        log.debug("[查询设备状态]设备状态为：START_SUCCESS，统一返回WORKING。");
        result[0] = CommandResult.QueryDeviceResultCode.WORKING;
        result[1] = CommandResult.QueryDeviceResultMsg.WORKING;
        // result[0] = CommandResult.QueryDeviceResultCode.START_SUCCESS;
        // result[1] = CommandResult.QueryDeviceResultMsg.START_SUCCESS;
        return result;

      case DictEnum.DeviceChannelStatus.WORK_DONE:
        log.debug("[查询设备状态]设备状态为：WORK_DONE，统一返回WORKING。");
        result[0] = CommandResult.QueryDeviceResultCode.WORKING;
        result[1] = CommandResult.QueryDeviceResultMsg.WORKING;
        // result[0] = CommandResult.QueryDeviceResultCode.WORK_DONE;
        // result[1] = CommandResult.QueryDeviceResultMsg.WORK_DONE;
        return result;

      case DictEnum.DeviceChannelStatus.WORKING:
        result[0] = CommandResult.QueryDeviceResultCode.WORKING;
        result[1] = CommandResult.QueryDeviceResultMsg.WORKING;
        return result;
      default:
        result[0] = CommandResult.PublicResultCode.UNKNOW_ERROR;
        result[1] = CommandResult.PublicResultCode.UNKNOW_ERROR;
        return result;
    }
  }

  public String getIpAddr(HttpServletRequest request)
  {
    String ip = request.getHeader("x-forwarded-for");
    // 如果用了代理，会出现这样的内容61.141.136.7, 14.17.37.72
    if (ip != null && ip.indexOf(",") != -1)
    {
      ip = ip.split(",")[0].trim();
    }
    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
    {
      ip = request.getHeader("Proxy-Client-IP");
    }
    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
    {
      ip = request.getHeader("WL-Proxy-Client-IP");
    }
    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
    {
      ip = request.getRemoteAddr();
    }
    return ip.equals("0:0:0:0:0:0:0:1") ? "127.0.0.1" : ip;
  }
}
